package struct.stack.realize;

/**
 * @author: 柒月
 * @date: 2025/5/2 下午7:38
 * @version: 1.0
 */
public class ArrayStack {

    private final int[] array;
    private int top;
    private final int capacity;

    public ArrayStack(int size) {
        array = new int[size];
        capacity = size;
        top = 0;
    }

    public boolean isEmpty() {
        return top == 0;
    }

    public int size() {
        return top;
    }

    public boolean isFull() {
        return top == capacity;
    }

    public void push(int value) {
        if (isFull()){
            throw new RuntimeException("Stack is full");
        }
        array[top++] = value;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("Stack is empty");
        }
        int value = array[top - 1];
        top--;
        return value;
    }

    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("Stack is empty");
        }
        return array[top - 1];
    }

}
